package net.realsee.service.wcs.data.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import net.realsee.common.utils.StringUtils;
import net.realsee.domain.wcs.data.WcsWorkCenterLocation;
import net.realsee.domain.wcs.data.bo.WcsWorkCenterLocationBo;
import net.realsee.domain.wcs.data.vo.WcsWorkCenterLocationVo;
import net.realsee.framework.core.domain.PageQuery;
import net.realsee.framework.core.page.TableDataInfo;
import net.realsee.mapper.wcs.data.WcsWorkCenterLocationMapper;
import net.realsee.service.wcs.data.IWcsWorkCenterLocationService;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 工位 库位关系Service业务层处理
 *
 * @author realsee
 * @date 2023-08-11
 */
@RequiredArgsConstructor
@Service
public class WcsWorkCenterLocationServiceImpl extends ServiceImpl<WcsWorkCenterLocationMapper, WcsWorkCenterLocation> implements IWcsWorkCenterLocationService {

    private final WcsWorkCenterLocationMapper baseMapper;

    /**
     * 查询工位 库位关系
     */
    @Override
    public WcsWorkCenterLocationVo selectVoById(Long id) {
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询工位 库位关系列表
     */
    @Override
    public TableDataInfo<WcsWorkCenterLocationVo> selectVoPage(WcsWorkCenterLocationBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<WcsWorkCenterLocation> lqw = buildVoQueryWrapper(bo);
        Page<WcsWorkCenterLocationVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询工位 库位关系列表
     */
    @Override
    public List<WcsWorkCenterLocationVo> selectVoList(WcsWorkCenterLocationBo bo) {
        LambdaQueryWrapper<WcsWorkCenterLocation> lqw = buildVoQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<WcsWorkCenterLocation> buildVoQueryWrapper(WcsWorkCenterLocationBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<WcsWorkCenterLocation> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getId() != null, WcsWorkCenterLocation::getId, bo.getId());
        lqw.eq(StringUtils.isNotBlank(bo.getWorkCenter()), WcsWorkCenterLocation::getWorkCenter, bo.getWorkCenter());
        lqw.eq(StringUtils.isNotBlank(bo.getLocation()), WcsWorkCenterLocation::getLocation, bo.getLocation());
        lqw.eq(StringUtils.isNotBlank(bo.getFuid()), WcsWorkCenterLocation::getFuid, bo.getFuid());
        lqw.eq(bo.getTextId() != null, WcsWorkCenterLocation::getTextId, bo.getTextId());
        lqw.eq(bo.getRefrenceId() != null, WcsWorkCenterLocation::getRefrenceId, bo.getRefrenceId());
        lqw.eq(bo.getUnitId() != null, WcsWorkCenterLocation::getUnitId, bo.getUnitId());
        lqw.eq(bo.getDelFlag() != null, WcsWorkCenterLocation::getDelFlag, bo.getDelFlag());
        lqw.eq(StringUtils.isNotBlank(bo.getCreateBy()), WcsWorkCenterLocation::getCreateBy, bo.getCreateBy());
        lqw.eq(bo.getCreateTime() != null, WcsWorkCenterLocation::getCreateTime, bo.getCreateTime());
        lqw.eq(StringUtils.isNotBlank(bo.getUpdateBy()), WcsWorkCenterLocation::getUpdateBy, bo.getUpdateBy());
        lqw.eq(bo.getUpdateTime() != null, WcsWorkCenterLocation::getUpdateTime, bo.getUpdateTime());
        lqw.eq(StringUtils.isNotBlank(bo.getDeleteBy()), WcsWorkCenterLocation::getDeleteBy, bo.getDeleteBy());
        lqw.eq(bo.getDeleteTime() != null, WcsWorkCenterLocation::getDeleteTime, bo.getDeleteTime());
        lqw.eq(bo.getRowVersionStamp() != null, WcsWorkCenterLocation::getRowVersionStamp, bo.getRowVersionStamp());
        lqw.eq(StringUtils.isNotBlank(bo.getRemark()), WcsWorkCenterLocation::getRemark, bo.getRemark());
        return lqw;
    }

    /**
     * 新增工位 库位关系
     */
    @Override
    public Boolean insertByBo(WcsWorkCenterLocationBo bo) {
        WcsWorkCenterLocation add = BeanUtil.toBean(bo, WcsWorkCenterLocation.class);
        return this.save(add);
    }

    /**
     * 修改工位 库位关系
     */
    @Override
    public Boolean updateByBo(WcsWorkCenterLocationBo bo) {
        WcsWorkCenterLocation update = BeanUtil.toBean(bo, WcsWorkCenterLocation.class);
        return this.updateById(update);
    }

    /**
     * 批量删除工位 库位关系
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids) {
        return this.removeBatchByIds(ids);
    }
}
